package modeleIndexation;

import java.util.ArrayList;
import java.util.List;

import Main.Mysql;

public class NoeudIndex {
	private int id;
	private String balise;
	private int ordre;
	private List<NoeudIndex> fils;

	public NoeudIndex(String balise, int ordre) {
		this.balise = balise;
		this.ordre = ordre;	
		this.fils = new ArrayList<NoeudIndex>();
	}
	
	public String getBalise() {
		return balise;
	}
	
	public int getOrdre() {
		return ordre;
	}
	
	public int getID() {
		return id;
	}
	
	public List<NoeudIndex> getFils() {
		return fils;
	}
	
	public void addFils(NoeudIndex fils) {
		this.fils.add(fils);
	}
	
	public void toBddfromDoc(int docPere) {
		Mysql bdd = Mysql.getInstance();
    	/* Insère le noeud dans la bdd */
    	String request = "INSERT INTO Noeuds (nomBalise, ordre) VALUES ('" + balise + "', " + ordre + ");";
		bdd.executeInsert(request);
		
  		String queryID = "SELECT LAST_INSERT_ID();";
		id = Integer.parseInt((String) bdd.getResultSet(bdd.executeSelect(queryID)).get(0).get(0));
		
    	/* Insère la relation doc-noeud dans la bdd */
    	request = "INSERT INTO DocNoeud (idDoc, idNoeud) VALUES (" + docPere + ", " + id + ");";
		bdd.executeInsert(request);
		
	
		for (int i=0;i<fils.size();i++) {
			fils.get(i).toBdd(id);
		}
	}
	
	public void toBdd(int noeudPere) {
		Mysql bdd = Mysql.getInstance();
    	/* Insère le noeud dans la bdd */
    	String request = "INSERT INTO Noeuds (nomBalise, ordre) VALUES ('" + balise + "', " + ordre + ");";
		bdd.executeInsert(request);
			
  		String queryID = "SELECT LAST_INSERT_ID();";
		id = Integer.parseInt((String) bdd.getResultSet(bdd.executeSelect(queryID)).get(0).get(0));
		
    	/* Insère la relation doc-noeud dans la bdd */
    	request = "INSERT INTO NoeudNoeud (idPere, idFils) VALUES (" + noeudPere + ", " + id + ");";
		bdd.executeInsert(request);
	
		for (int i=0;i<fils.size();i++) {
			fils.get(i).toBdd(id);
		}
	}
}
